package yt;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

public class GeneratorServiceEntity {

    @Test
    public void generateCode() {
        String packageName = "yt.MajorClass"; /* 【必填】设置父包名称 */
        boolean serviceNameStartWithI = false;//user -> UserService 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, "users"); /* 【必填】添加要生成的表可多个 */
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://localhost:3306/yt01";       /* 【必填】设置数据库 */
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")    /* 【必填】账号 */
                .setPassword("root")  /* 【必填】密码 */
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true) // 实体是否为lombok模型（默认 false)
                .setNaming(NamingStrategy.underline_to_camel)	// 命名法转换
                .setInclude(tableNames);
        config.setActiveRecord(false)
                .setAuthor("hlt")
                .setOutputDir("F://demo1202//src//main//java") /* 【必填】设置路径 */
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()		/* 【选填】在这里可以修改生成各层的包名 */
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
